var _api = _interopRequireDefault(require("../../utils/api")), _contactUtil = require("../../utils/contact-util");

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _defineProperty(t, e, i) {
    return e in t ? Object.defineProperty(t, e, {
        value: i,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = i, t;
}

var arrJoin = function(e) {
    return function(t) {
        return t.map(function(t) {
            return t[e];
        }).join(",");
    };
};

Component({
    properties: {
        mode: String,
        multiple: {
            type: Boolean,
            value: !0
        },
        needAdd: {
            type: Boolean,
            value: !0
        },
        list: {
            type: Array,
            value: [],
            observer: "dealList"
        },
        catid: String,
        guid: String
    },
    data: {
        emptyTip: "还没有添加过部门呢..."
    },
    ready: function() {
        this.firstChange = !0, this.dialog = this.selectComponent("#dialog");
    },
    methods: {
        dealList: function() {
            var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : [], a = [], n = [], e = this.data.catid;
            !function i(t) {
                t.forEach(function(e) {
                    a.push(e);
                    var t = e.children;
                    t && t.length && (n.push({
                        catid: e.catid
                    }), t.forEach(function(t) {
                        return t.parentid = e.catid;
                    }), i(t));
                });
            }(t), this.setData({
                childCat: n,
                checkboxItems: a
            }), this.checkboxChange({
                detail: {
                    value: e.split(",")
                }
            });
        },
        getCheckVal: function(t) {
            var e = this.data, i = e.multiple, a = e.checkList, n = e.mode;
            if (i) {
                if (!t.length) return;
                "contacts" === n && 2 === t.length && this.firstChange && (t.shift(), this.firstChange = !1);
            } else 2 === t.length && t.shift(), t.length || (t = [ a[0].catid ]);
            return t;
        },
        checkboxChange: function(t) {
            var e = t.detail.value;
            if (e = this.getCheckVal(e)) {
                var i, a = this.data.checkboxItems;
                a.forEach(function(t) {
                    t.checked = !1, e.includes(t.catid) && (t.checked = !0);
                }), i = a.filter(function(t) {
                    return t.checked;
                }), this.setData({
                    checkboxItems: a,
                    checkList: i
                });
            }
        },
        onDeselect: function(t) {
            var e = t.currentTarget.dataset.catid, i = this.data.checkList.filter(function(t) {
                return t.catid !== e;
            }).map(function(t) {
                return t.catid;
            });
            this.checkboxChange({
                detail: {
                    value: i
                }
            });
        },
        openChild: function(t) {
            var e, i = t.currentTarget.dataset, a = i.catid, n = i.index, c = this.data, r = c.childCat, o = c.checkboxItems;
            this.setData((_defineProperty(e = {}, "checkboxItems[".concat(n, "].isopen"), !o[n].isopen), 
            _defineProperty(e, "childCat", r.map(function(t) {
                return t.catid === a && (t.isopen = !t.isopen), t;
            })), e));
        },
        onConfirm: function(t) {
            var e = t.detail.value;
            this.addRequest(e);
        },
        addDerpartment: function() {
            this.dialog.show();
        },
        addRequest: function(t) {
            var e = this, i = this.data, a = i.checkList, n = i.guid;
            _api.default.minicard.category.add({
                data: {
                    guid: n,
                    name: t,
                    catid: 0
                }
            }).then(function(t) {
                wx.showToast({
                    title: "添加成功",
                    duration: 500
                }), e.setData({
                    catid: arrJoin("catid")(a)
                }), e.$emit("contact.user.change"), e.dialog.hide(), e.triggerEvent("add", {}, {});
            });
        },
        onOk: function() {
            var t = this.data.checkList;
            this.triggerEvent("confirm", {
                value: {
                    catid: arrJoin("catid")(t) || _contactUtil.ROOT_CATID,
                    name: arrJoin("name")(t)
                }
            }, {});
        }
    }
});